import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    token: '', // 用于存储 token
  },
  mutations: {
    setToken(state, token) {
      state.token = token; // 更新 token
    },
  },
  actions: {
    saveToken({ commit }, token) {
      commit('setToken', token); // 提交 mutation
      window.sessionStorage.setItem('token', token); // 存储在 sessionStorage
    },
    loadToken({ commit }) {
      const token = window.sessionStorage.getItem('token'); // 从 sessionStorage 获取 token
      if (token) {
        commit('setToken', token);
      }
    },
  },
});

export default store;
